<?php
class News_IndexController extends Zendme_Controller_Action{
	
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 2,
									'pageRange' => 2,
								  ); 
	protected $_namespace;
	
	public function init(){
		
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] 
									 . '/' . $this->_arrParam['controller'];		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index';
							 
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//$ssFilter->unsetAll();
		if(empty($ssFilter->col)){
			$ssFilter->keySearch 	= '';
			$ssFilter->lang 		= '';
		}
		$this->_arrParam['ssFilter']['keySearch'] 	= $ssFilter->keySearch;	
		$this->_arrParam['ssFilter']['lang'] 		= $this->_arrParam['lang'];	

		
		$info = new Zendme_System_Info();
		
		$language = $info->getLanguage('admin');
		if(empty($language)){
			$language = 'vi';
		}
		$translate = array(
							'adapter' => 'tmx',
							'content' => LANG_PATH . '/' . $language . '/admin/default.language.tmx',
							'locale' => $language,
							);
		$translate = new Zend_Translate($translate);
		Zend_Registry::set('Zend_Translate', $translate);
			
		Zend_Layout::getMvcInstance()->assign('bg', 'bag-image-page');
		Zend_Layout::getMvcInstance()->assign('current', 'news');	
		
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;		
		$template_path = TEMPLATE_PATH . "/front/system1";
		$this->loadTemplate($template_path,'template.ini','template');		
		
		
	}
	public function indexAction(){	
//		$tblNews = new Zendme_Models_NewsItem();
//		$this->view->Items = $tblNews->listItem($this->_arrParam,array('task'=>'public-index'));
//		$this->view->total = $tblNews->countItem($this->_arrParam,array('task'=>'public-index'));		
//		$totalItem  = $tblNews->countItem($this->_arrParam,array('task'=>'public-index'));
//		$paginator = new Zendme_Paginator();
//		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
//		$this->view->Title = 'TIN TỨC';
//		$this->view->headTitle($this->view->Title,true);

		$this->view->headTitle('Tin tức');
		$this->view->headMeta()->appendName('description', ''); 
		$this->view->headMeta()->appendName('keywords', ''); 
		
	}	
	public function filterAction(){
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		if($this->_arrParam['type'] == 'search'){			
			if($this->_arrParam['key'] == 1){
				$ssFilter->keySearch = trim($this->_arrParam['keySearch']);
			}else{
				$ssFilter->keySearch = '';
			}
		}		
		$this->_redirect($this->_actionMain);
		$this->_helper->viewRenderer->setNoRender();
	}
	public function categoryAction(){
				
//		$tblNews = new Zendme_Models_NewsItem();
//		$this->view->Items = $tblNews->listItem($this->_arrParam,array('task'=>'public-category'));
//		$tblCategoryMenu = new Zendme_Models_CatItemNews();
//		$this->view->tblCategoryMenu = $tblCategoryMenu->getItem($this->_arrParam,null);
//		$this->view->tblParentMenu = $tblCategoryMenu->getParent($this->_arrParam,null);
//		$this->view->Title = 'News >> ' . $this->view->Items[0]['category_name'];
//		$this->view->headTitle($this->view->Title,true);
//		$totalItem  = $tblNews->countItem($this->_arrParam,array('task'=>'public-category'));
//		$paginator = new Zendme_Paginator();
//		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
//		$this->view->total = $tblNews->countItem($this->_arrParam,array('task'=>'public-category'));

		$mdlCategory=new Zendme_Models_CategoryNews();
		$category=$mdlCategory->getItem($this->_arrParam,array('task'=>'public'));
		$this->view->category=$category;
		
		$mdlNewItem=new Zendme_Models_NewsItem();
		$items=$mdlNewItem->listItem($this->_arrParam,array('task'=>'public-category','paginator'=>$this->_paginator));
		$this->view->items=$items;
		
		$totalItem  = $mdlNewItem->countItem($this->_arrParam,array('task'=>'public-category'));	
		$paginator = new Zendme_Paginator();
		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
		$this->view->total = $mdlNewItem->countItem($this->_arrParam,array('task'=>'public-category'));
		
		$this->view->headTitle($category['name']);
		if(!empty($category['meta_description']))
		$this->view->headMeta()->appendName('description', $category['meta_description']); 
		if(!empty($category['meta_keyword']))
		$this->view->headMeta()->appendName('keywords', $category['meta_keyword']); 
		
	}
	public function detailAction(){		
//		$tblDetail = new Zendme_Models_NewsItem();
//		$this->view->Item = $tblDetail->getItem($this->_arrParam,array('task'=>'public-detail'));
//      $this->view->Cat = $tblDetail->listItem($this->_arrParam,array('task'=>'public-detail'));
//		$this->view->Title = $this->view->Item['name'];
//		$this->view->headTitle($this->view->Title,true);
//		$this->view->headMeta(true)->setName('description',$this->view->Item['name']);
		
		$mdlNewItem=new Zendme_Models_NewsItem();
		$item=$mdlNewItem->getItem($this->_arrParam,array('task'=>'public-detail'));
		$this->view->item=$item;
		
		$mdlCategory=new Zendme_Models_CategoryNews();
		$this->_arrParam['id']=$item['cat_id'];
		$category=$mdlCategory->getItem($this->_arrParam,array('task'=>'public-id'));
		$this->view->category=$category;
		
		$this->view->headTitle($item['name']);
		if(!empty($item['meta_description']))
		$this->view->headMeta()->appendName('description', $item['meta_description']); 
		if(!empty($item['meta_keyword']))
		$this->view->headMeta()->appendName('keywords', $item['meta_keyword']); 
	}
	public function commentAction(){
		$tblComments = new Zendme_Models_Comments();		
		if($this->_request->isPost()){
			$validator = new News_Form_ValidateComment($this->_arrParam);
			if($validator->isError() == true){
				$this->view->errors = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
			}else{
				$arrParam = $validator->getData(array('upload'=>true));
				$tblComments->saveItem($arrParam,array('task'=>'add-comment'));
				//$this->_redirect($this->_currentController . '/success');
			}
		}
		$this->_helper->layout->disableLayout();
	}
	
	
	
	public function successAction(){
		$this->_helper->layout->disableLayout();
	}
	public function exelAction(){
		$dir =  FILES_PATH . '/exels/';
		require_once $dir .'excel_reader2.php';
		$data = new Spreadsheet_Excel_Reader($dir . "test2.xls");
		$files = $data->sheets[0]['cells'];
		unset($files[1]);
		$this->_arrParam['files'] = $files;
		$tblExel = new Zendme_Models_Exels();
		$list = $tblExel->saveItem($this->_arrParam, array('task' => 'admin-add'));
	}
	public function importExelAction(){
		$tblExel = new Zendme_Models_Exels();
		if($this->_request->isPost()){
			$validator = new News_Form_ValidateExel($this->_arrParam);
			if($validator->isError() == true){
				$this->view->errors = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
				
			}else{				
				$arrParam = $validator->getData(array('upload'=>true));
				$filesname = $arrParam['fileexel'];
				$dir =  FILES_PATH . '/exels/';
				require_once $dir .'excel_reader2.php';
				$data = new Spreadsheet_Excel_Reader($dir . $filesname);
				$files = $data->sheets[0]['cells'];		
				unset($files[1]);
				$arrParam['files'] = $files;
				$tblExel = new Zendme_Models_Exels();
				$list = $tblExel->saveItem2($arrParam, array('task' => 'admin-add'));
				$tblDelete = $tblExel->delete($arrParam,array('task'=>'add-delete'));
				//$this->_redirect($this->_currentController . '/success');
			}
		}
		//$this->_helper->layout->disableLayout();
	}
	public function importAction(){
		$tblExel = new Zendme_Models_Exels();
		if($this->_request->isPost()){
			$validator = new News_Form_ValidateExel($this->_arrParam);
			if($validator->isError() == true){
				$this->view->errors = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
				
			}else{				
				$arrParam = $validator->getData(array('upload'=>true));
				$filesname = $arrParam['fileexel'];
				$dir =  FILES_PATH . '/exels/';
				require_once $dir .'excel_reader2.php';
				$data = new Spreadsheet_Excel_Reader($dir . $filesname);
				$files = $data->sheets[0]['cells'];		
				unset($files[1]);
				$arrParam['files'] = $files;
				$tblExel = new Zendme_Models_Exels();
				$list = $tblExel->saveItem2($arrParam, array('task' => 'admin-add'));
				$tblDelete = $tblExel->delete($arrParam,array('task'=>'add-delete'));
				//$this->_redirect($this->_currentController . '/success');
			}
		}
		$this->_helper->layout->disableLayout();
		
	}
	public function uploadExelAction(){
		
	}
	public function newNewsAction(){
		
	}
	public function menuAction(){
		
	}
}